package com.botann.projects.utils;

import java.io.InputStream;
import java.util.Properties;

/**
 * Charging
 * PropertiesUtils
 * Created by 刘山国 on 2018/11/20.
 * Copyright (c) 2017 伯坦科技. All rights reserved
 */
public class PropertiesUtils {
    private static Properties properties;

    private static  Properties getProperties () {
        if (properties == null) return intancePoperties();
        return properties;
    }

    private static synchronized Properties intancePoperties() {
        properties = new Properties();
        try {
            InputStream in = (new PropertiesUtils()).getClass().getResourceAsStream("/project.properties");
            properties.load(in);//从输入流中读取属性列表（键和元素对）。
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return properties;
    }


    /**
     * 是否是生产环境(正式)
     * @return Boolean production
     */
    public static Boolean isProduction() {
        return getProperties().getProperty("production").equals("true");
    }

    /**
     * 获取 property
     * @param name key
     * @return String property
     */
    public static String getProperty(String name) {
        return getProperties().getProperty(name);
    }

    /**
     * 获取property 自动根据 production  使用 name or name.test
     * @param name key
     * @return String property
     */
    public static String getAutoProperty(String name) {
        if (isProduction()) {
            return getProperty(name);
        } else {
            return getProperty(name+".test");
        }
    }

}
